import { ModuleType } from "@/type";
import { getAttribute } from "@/utils/tool";

const deepGetNodeXml = (list: ModuleType[]) => {
  let xml = "";
  for (const item of list) {
    xml += getNodeXml(item);
  }
  return xml;
};

const getNodeXml = (node: ModuleType) => {
  const attribute = getAttribute(node);
  const value = node.layout ? "" : node.value;
  const content = deepGetNodeXml(node.children);
  if (!node.layout && !value) {
    return `<${node.type}${attribute} />`;
  }
  return `<${node.type}${attribute}>${value}${content}</${node.type}>`;
};

export default deepGetNodeXml;
